<template>
  <div class="login-container" v-loading="loading">
    <div class="login-card">
      <div class="login-title-1">MY博客</div>
      <div class="login-title-2">管理员登录</div>
      <!-- 登录表单 -->
      <el-form
          status-icon
          :model="loginForm"
          :rules="rules"
          ref="ruleForm"
          class="login-form"
      >
        <!-- 用户名输入框 -->
        <el-form-item prop="username">
          <el-input
              v-model="loginForm.username"
              prefix-icon="el-icon-user-solid"
              placeholder="用户名"
              @keyup.enter.native="login"
          />
        </el-form-item>
        <!-- 密码输入框 -->
        <el-form-item prop="password">
          <el-input
              v-model="loginForm.password"
              prefix-icon="el-icon-lock"
              show-password
              placeholder="密码"
              @keyup.enter.native="login"
          />
        </el-form-item>
      </el-form>
      <!-- 登录按钮 -->
      <el-button type="primary" @click="login">登录</el-button>
    </div>
  </div>
</template>

<script>

export default {
  methods: {
    login() {
      this.loading = true
      this.$refs.ruleForm.validate(valid => {
        if (valid) {
          const that = this;
          //发送登录请求
          let param = new URLSearchParams();
          param.append("username", that.loginForm.username);
          param.append("password", that.loginForm.password);
          this.postRequest(this.api.login, param).then(data => {
            if (data) {
              // 登录后保存用户信息
              sessionStorage.setItem("user", JSON.stringify(data.data));
              that.$message.success("登录成功");
              let tempPath = that.$route.query.to
              console.log(that.$route.query.to)
              if (tempPath){
                that.$router.push({path:tempPath})
              }else {
                that.$router.push({path: "/"});
              }
            }
          })
        }
        setTimeout(() => {
          this.loading = false
        }, 1000)
      });
    }
  },
  data: function () {
    return {
      loading: false,
      loginForm: {
        username: "",
        password: ""
      },
      rules: {
        username: [
          {required: true, message: "用户名不能为空", trigger: "blur"}
        ],
        password: [{required: true, message: "密码不能为空", trigger: "blur"}]
      }
    };
  }
};
</script>

<style scoped>
.login-container {
  position: absolute;
  top: 0;
  bottom: 0;
  right: 0;
  left: 0;
  background: url(http://120.24.182.34:8888/group1/M00/00/00/rBg1pmJxFPaARD74AAq5toNWrfI915.jpg) center center /
    cover no-repeat;
}

.login-card {
  background: #fff;
  margin: 0 auto;
  padding: 170px 60px 180px;
  width: 350px;
  border-radius: 1%;
}

.login-title-1{
  position: absolute;
  top: 10vh;
  letter-spacing: 1rem;
  color: #303133;
  font-weight: bold;
  font-size: 2rem;
}

.login-title-2 {
  color: #303133;
  font-weight: bold;
  font-size: 1rem;
}

.login-form {
  margin-top: 1.2rem;
}

.login-card button {
  margin-top: 1rem;
  width: 100%;
}
</style>
